package com.lie.flat.service.connect;


import com.lie.flat.entity.CodeRequest;
import com.lie.flat.entity.CodeResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class CodeService {

    @Autowired
    private RestTemplate restTemplateCode;

    /**
     * 向第三方接口发送手机验证码请求
     *
     * @param codeRequest
     * @return
     */
    public CodeResponse jsonReqCode(CodeRequest codeRequest) {
        final String url = "http://139.196.200.240:8070/creditCardTest/getMobilePhoneCode";
        CodeResponse codeResponse = null;

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
        HttpEntity<CodeRequest> entity = new HttpEntity<>(codeRequest, httpHeaders);
        try {
            ResponseEntity<CodeResponse> resp = restTemplateCode.postForEntity(url, entity, CodeResponse.class);
            codeResponse = resp.getBody();
        } catch (Exception e) {
            log.error("请求失败", e);
        }
        return codeResponse;
    }
}
